/*	This program produces Appendix Table F.1 */

***** Set directories 
local dir_output 	"~/Dropbox/Retirement gaming/output/dataverse"

forvalues i=1/3 {
	tempfile bote_scenario`i'
	tempfile bote_counter4_scenario`i'
}


** Parameters **

* interest rates
global int1 = 0.02
global int2 = 0.00
global int3 = 0.04

* retirement age
global retirage1 = 60
global retirage2 = 63
global retirage3 = 65
global retirage4 = 63 // counterfactual with 30 yrs contribution but intermitent service

* replacement rate
global RR1 = 0.45
global RR2 = 0.54
global RR3 = 0.60
global RR4 = 0.45 // counterfactual with 30 yrs contribution but intermitent service

* tax rate
global tau = 0.22625

* duration of each period
global T1d1 = 20 // add 10, count from 11 to 30
global T1d2 = 23 // add 10, count from 11 to 33
global T1d3 = 25 // add 10, count from 11 to 35
global T1d4 = 30 // counterfactual with 30 yrs contribution, period 1 lasts 30 yers but 20 yrs service

global T2d = 10 

global T3d1 = 22
global T3d2 = 20 
global T3d3 = 19
global T3d4 = 20 // counterfactual with 30 yrs contribution, retires at 63

* Prices and exchange rates
global cpiadj1519 = 1.08 // to convert 2015 usd to 2019 usd
global uypeso15 = 29.78 // USD to UY$ exchange rate 2015
global uypeso19 = 37.585 // USD to UY$ exchange rate 2015
global pib_usd19 = 1975921080/($uypeso19 * 1000) // GDP in 2019, millions of USD


* estimated parameters earnings and retirees
global lambda =  0.0484 // 0.4 * coef(0-3) + 0.6 * coef(4-7) = 0.0484
global lambdar = $lambda / (1+ $lambda ) // estimated lambda/(1+lambda) = percent response
global w2_uy15 = 9.419 * 1000 // average self-empl monthly reported earnings, in UY$ 2015  (from our main sample, reported earnings ages 54-57)
global w2_usd19 = ($w2_uy15 / 29.78 )* $cpiadj1519 // average self-empl monthly reported earnings in USD 2019 (convert to USD 2015, then adjust for CPI to bring to 2019 USD) 
global deltaw = $lambdar * $w2_usd19 * 12 // estimated change in reported earnings self-employed between periods 1 and 2
global shareself = 2585/(6125+2585) // share self-employed in our sample 54-57 year-olds
*global shareself = 675/(675+1632) // share self-employed in our sample retirees (almost the same)

 
* average number of retirees per year (from contributory retirement industria y comercio)
global totretir = 259085 // from BPS iv.-prestaciones-de-pasividad, cantidad jubilaciones industria y comercio April 2021 
global selfretir = $totretir * $shareself


** Scenario 1 **
postfile  scenario1 counterfact irate replrate rrpv_tot taupv_tot term1 term2 netchange usdpc usdtot pctpib using `bote_scenario1', replace 
 forvalues i=1/3 {
	 forvalues c=1/3 { //counterfactuals 1 to 3 (retir age, RR, duration)
		local rr=${RR`c'}
		local ir=${int`i'}
		local irate=round(`ir'*100)
		local tau=$tau
		local term2=$lambdar
		local w2w1=$deltaw 
		local numretir= $selfretir
		local pib=$pib_usd19
		* sum rrpv
		clear 
		set obs ${T3d`c'}
		g rrpv = `rr' * 1/((1+`ir')^(_n-1)) 
		g rrpv_tot = sum(rrpv)
		local rrpv_tot=rrpv_tot[_N]
		display `rrpv_tot'	
		* sum taupv
		clear 
		set obs ${T2d}
		g taupv = `tau' * ((1+`ir')^(_n))
		g taupv_tot = sum(taupv)
		local taupv_tot=taupv_tot[_N]
		display `taupv_tot'		
		* first term
		local term1 = 1 - `taupv_tot'/`rrpv_tot'	
		* net change
		local netchange = `term1'*`term2'
		* change in dollars per individual per year (only numerator)
		local usdpc = (`rrpv_tot' - `taupv_tot') * `w2w1' 
		* change in mill. dollars total (only numerator)
		local usdtot = `usdpc' * `numretir' / 1000000
		local pctpib = `usdtot' * 100 / `pib'
	post scenario1 (`c') (`irate') (`rr') (`rrpv_tot') (`taupv_tot') (`term1') (`term2') (`netchange') (`usdpc') (`usdtot') (`pctpib')
	}
 }
postclose scenario1 
clear


** Scenario 2 **
postfile  scenario2 counterfact irate replrate rrpv_tot taupv_tot term1 term2 netchange usdpc usdtot pctpib using `bote_scenario2', replace 
 forvalues i=1/3 {
	forvalues c=1/3 { //counterfactuals 1 to 3 (retir age, RR, duration)
		local rr=${RR`c'}
		local ir=${int`i'}
		local irate=round(`ir'*100)
		local tau=$tau
		local term2=$lambdar
		local w2w1=$deltaw 
		local numretir= $selfretir
		local pib=$pib_usd19
		local t2=$T2d
		* sum rrpv
		clear 
		set obs ${T3d`c'}
		g rrpv = `rr' * 1/((1+`ir')^(_n-1)) 
		g rrpv_tot = sum(rrpv)
		local rrpv_tot=rrpv_tot[_N]
		display `rrpv_tot'	
		* sum taupv
		clear 
		set obs ${T1d`c'}
		g taupv = `tau' * ((1+`ir')^(_n + `t2'))
		g taupv_tot = sum(taupv)
		local taupv_tot=taupv_tot[_N]
		display `taupv_tot'		
		* first term
		local term1 = `taupv_tot'/`rrpv_tot'	
		* net change
		local netchange = `term1'*`term2'
		* change in dollars per individual per year (only numerator)
		local usdpc =  `taupv_tot'	* `w2w1' 
		* change in mill. dollars total (only numerator)
		local usdtot = `usdpc' * `numretir' / 1000000
		local pctpib = `usdtot' * 100 / `pib'
		post scenario2 (`c') (`irate') (`rr') (`rrpv_tot') (`taupv_tot') (`term1') (`term2') (`netchange') (`usdpc') (`usdtot') (`pctpib')
	 }
 }	 
postclose scenario2
clear

** Scenario 3 **
postfile  scenario3 counterfact irate replrate rrpv_tot taupv_tot term1 term2 netchange usdpc usdtot pctpib using `bote_scenario3', replace 
 forvalues i=1/3 {
	forvalues c=1/3 { //counterfactuals 1 to 3 (retir age, RR, duration)
		local rr=${RR`c'}
		local ir=${int`i'}
		local irate=round(`ir'*100)
		local tau=$tau
		local term2=$lambdar
		local w2w1=$deltaw 
		local numretir= $selfretir
		local pib=$pib_usd19
		local t2=$T2d
		* sum rrpv
		clear 
		set obs ${T3d`c'}
		g rrpv = `rr' * 1/((1+`ir')^(_n-1)) 
		g rrpv_tot = sum(rrpv)
		local rrpv_tot=rrpv_tot[_N]
		display `rrpv_tot'	
		* First tau sum (T1)
		clear 
		set obs ${T1d`c'}
		g taupv = `tau' * ((1+`ir')^(_n + `t2'))
		g taupv_tot = sum(taupv)
		local taupv_tot=taupv_tot[_N]
		display `taupv_tot'		
		* Second tau sum (T2)
		clear 
		set obs ${T2d}
		g taupv2 = `tau' * ((1+`ir')^_n)
		g taupv2_tot = sum(taupv2)
		local taupv2_tot=taupv2_tot[_N]
		display `taupv2_tot'		
		* first term
		local term1 = 1 +(`taupv_tot'-`taupv2_tot')/`rrpv_tot'	
		* net change
		local netchange = `term1'*`term2'/2
		* change in dollars per individual per year (only numerator)
		local usdpc =  (`rrpv_tot'+`taupv_tot'-`taupv2_tot')* `w2w1'/2
		* change in mill. dollars total (only numerator)
		local usdtot = `usdpc' * `numretir' / 1000000
		local pctpib = `usdtot' * 100 / `pib'
		post scenario3 (`c') (`irate') (`rr') (`rrpv_tot') (`taupv_tot') (`term1') (`term2') (`netchange') (`usdpc') (`usdtot') (`pctpib')
	 }
 }	 
postclose scenario3
clear


** ALL SCENARIOS WITH COUNTERFACTUAL 4 (INTERMITENT CONTRIBUTION HISTORY) **
** Counterfactual 4, Scenario 1 **
 postfile  scenario1 counterfact irate replrate rrpv_tot taupv_tot term1 term2 netchange usdpc usdtot pctpib using `bote_counter4_scenario1', replace 
	local c=4 // counterfactual 4
	local i=1 // interest rate 0.02
	*parameters
	local rr=${RR`c'}
	local ir=${int`i'}
	disp `ir'
	local irate=round(`ir'*100)
	local tau=$tau
	local term2=$lambdar
	local w2w1=$deltaw 
	local numretir= $selfretir
	local pib=$pib_usd19
	* sum rrpv (period 3)
	clear 
	set obs ${T3d`c'}
	g rrpv = `rr' * 1/((1+`ir')^(_n-1)) 
	g rrpv_tot = sum(rrpv)
	local rrpv_tot=rrpv_tot[_N]
	display `rrpv_tot'	
	* sum taupv (period 2)
	clear 
	set obs ${T2d}
	g taupv = `tau' * ((1+`ir')^(_n))
	g taupv_tot = sum(taupv)
	local taupv_tot=taupv_tot[_N]
	display `taupv_tot'		
	* first term
	local term1 = 1 - `taupv_tot'/`rrpv_tot'	
	* net change
	local netchange = `term1'*`term2'
	* change in dollars per individual per year (only numerator)
	local usdpc = (`rrpv_tot' - `taupv_tot') * `w2w1' 
	* change in mill. dollars total (only numerator)
	local usdtot = `usdpc' * `numretir' / 1000000
	local pctpib = `usdtot' * 100 / `pib'
post scenario1 (`c') (`irate') (`rr') (`rrpv_tot') (`taupv_tot') (`term1') (`term2') (`netchange') (`usdpc') (`usdtot') (`pctpib')
postclose scenario1 
clear

** Counterfactual 4, Scenario 2 **
postfile  scenario2 counterfact irate replrate rrpv_tot taupv_tot term1 term2 netchange usdpc usdtot pctpib using `bote_counter4_scenario2', replace 
	local c=4 // counterfactual 4
	local i=1 // interest rate 0.02
	*parameters
	local rr=${RR`c'}
	local ir=${int`i'}
	local irate=round(`ir'*100)
	local tau=$tau
	local term2=$lambdar
	local w2w1=$deltaw 
	local numretir= $selfretir
	local pib=$pib_usd19
	local t2=$T2d
	* sum rrpv (period 3)
	clear 
	set obs ${T3d`c'}
	g rrpv = `rr' * 1/((1+`ir')^(_n-1)) 
	g rrpv_tot = sum(rrpv)
	local rrpv_tot=rrpv_tot[_N]
	display `rrpv_tot'	
	* sum taupv (period 1)
	clear 
	set obs ${T1d`c'}
	g year=_n
	g notmult3=(mod(year,3)>0) // marks years of contribution
	g float taupv = `tau' * ((1+`ir')^(_n + `t2')) * notmult3 // 0 if not a year of contribution
	g taupv_tot = sum(taupv)
	local taupv_tot=taupv_tot[_N]
	display `taupv_tot'		
	* first term
	local term1 = `taupv_tot'/`rrpv_tot'	
	* net change
	local netchange = `term1'*`term2'
	* change in dollars per individual per year (only numerator)
	local usdpc =  `taupv_tot'	* `w2w1' 
	* change in mill. dollars total (only numerator)
	local usdtot = `usdpc' * `numretir' / 1000000
	local pctpib = `usdtot' * 100 / `pib'
post scenario2 (`c') (`irate') (`rr') (`rrpv_tot') (`taupv_tot') (`term1') (`term2') (`netchange') (`usdpc') (`usdtot') (`pctpib')
postclose scenario2
clear

** Counterfactual 4, Scenario 3 **
postfile  scenario3 counterfact irate replrate rrpv_tot taupv_tot term1 term2 netchange usdpc usdtot pctpib using `bote_counter4_scenario3', replace 
	local c=4 // counterfactual 4
	local i=1 // interest rate 0.02
	*parameters
	local rr=${RR`c'}
	local ir=${int`i'}
	local irate=round(`ir'*100)
	local tau=$tau
	local term2=$lambdar
	local w2w1=$deltaw 
	local numretir= $selfretir
	local pib=$pib_usd19
	local t2=$T2d
	* sum rrpv
	clear 
	set obs ${T3d`c'}
	g rrpv = `rr' * 1/((1+`ir')^(_n-1)) 
	g rrpv_tot = sum(rrpv)
	local rrpv_tot=rrpv_tot[_N]
	display `rrpv_tot'	
	* First tau sum (T1)
	clear 
	set obs ${T1d`c'}
	g year=_n
	g notmult3=(mod(year,3)>0) // marks years of contribution
	g float taupv = `tau' * ((1+`ir')^(_n + `t2')) // 0 if not a year of contribution
	g taupv_tot = sum(taupv)
	local taupv_tot=taupv_tot[_N]
	display `taupv_tot'		
	* Second tau sum (T2)
	clear 
	set obs ${T2d}
	g taupv2 = `tau' * ((1+`ir')^_n)
	g taupv2_tot = sum(taupv2)
	local taupv2_tot=taupv2_tot[_N]
	display `taupv2_tot'		
	* first term
	local term1 = 1 +(`taupv_tot'-`taupv2_tot')/`rrpv_tot'	
	* net change
	local netchange = `term1'*`term2'/2
	* change in dollars per individual per year (only numerator)
	local usdpc =  (`rrpv_tot'+`taupv_tot'-`taupv2_tot')* `w2w1'/2
	* change in mill. dollars total (only numerator)
	local usdtot = `usdpc' * `numretir' / 1000000
	local pctpib = `usdtot' * 100 / `pib'
post scenario3 (`c') (`irate') (`rr') (`rrpv_tot') (`taupv_tot') (`term1') (`term2') (`netchange') (`usdpc') (`usdtot') (`pctpib')
postclose scenario3
clear


*****************************************
* TABLE
*****************************************

use `bote_scenario1', clear
g scenario=1
forvalues s=2/3 {
	append using  `bote_scenario`s''
	replace scenario=`s' if scenario==.
}
forvalues s=1/3 {
	append using  `bote_counter4_scenario`s''
	replace scenario=`s' if scenario==.
}
 
replace netchange=netchange*100
 
g service=.
forvalues c=1/3 {
	replace service = ${T1d`c'} + $T2d if counterfact==`c'
}
replace service = ${T1d4}*(2/3) + $T2d if counterfact==4

g retirage=.
forvalues c=1/4 {
	replace retirage=${retirage`c'} if counterfact==`c'
}

g yrs1=.
forvalues c=1/4{
	replace yrs1 = ${T1d`c'} if counterfact==`c'
}

g yrs2 = $T2d

g yrs3=.
forvalues c=1/4 {
	replace yrs3 = ${T3d`c'} if counterfact==`c'
}

g tau=$tau

g intrate=irate/100

g lambda= $lambda


label var retirage "Retirement age"
label var service "Service years"
label var replrate "Replacement rate"
label var yrs1 "Years period 1"
label var yrs2 "Years period 2"
label var yrs3 "Years period 3"
label var tau "Payroll tax rate ($\tau$)" 

label var usdtot "Millions USD"
label var pctpib "Percentage points GDP"
label var netchange "Pct. points spending self-empl. pensions"
label var intrate "Interest rate"
label var lambda "Change in reported earnings ($\lambda$)" 

** BACK OF THE ENVELOPE TABLE **
** PART 1: PARAMETERS
* Robustness retirement age
estimates clear
forvalues c=1/4 { 
	eststo: quietly estpost summarize retirage service replrate yrs1 yrs2 yrs3 tau intrate lambda if counterfact==`c' & irate==2, casewise esample
}
* Robustness interest rate
foreach intrate in 0 2 4 { 
	eststo: quietly estpost summarize retirage service replrate yrs1 yrs2 yrs3 tau intrate lambda if counterfact==2 &  irate==`intrate', casewise esample
}
*Table
esttab using "`dir_output'/tableF1a.tex", ///
	cells("mean(fmt(0 0 3 0 0 0 3 3 3))")  nostar nonote label nonumber nodepvar ///
	style(tex) replace f nogaps nomtitles noobs nolines collabels(,none)
** PART 2: ESTIMATES by scenario
forvalues s = 1/3 {
	estimates clear
	* Robustness retirement age
	forvalues c=1/4 { 
		eststo: quietly estpost summarize netchange usdtot pctpib  if counterfact==`c' & irate==2 & scenario==`s', casewise esample
	}
	* Robustness interest rate
	foreach intrate in 0 2 4 { 
		eststo: quietly estpost summarize netchange usdtot pctpib  if counterfact==2 & irate==`intrate' & scenario==`s', casewise esample
	}
	*Table
	esttab using "`dir_output'/tableF1b_scen`s'.tex", ///
		cells("mean(fmt(2 2 2))")  nostar nonote label nonumber nodepvar ///
		style(tex) replace f nogaps nomtitles noobs	nolines collabels(,none)
}	


clear all
exit









